package com.playball.lottery;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
//modify by rain remove jPush
//import cn.jpush.android.api.JPushInterface;


import cn.jpush.android.api.JPushInterface;

import com.playball.lottery.frm.yhzx.LoginActivity;
import com.playball.lottery.util.ExampleUtil;
import com.playball.lottery.util.Logger;
import com.playball.lottery.util.cache.MyPreference;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "MyReceiver";

private NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {
   if (null == nm) {
       nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   }
    
   Bundle bundle = intent.getExtras();
   //Logger.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + bundle.toString());
    
   if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
       //Logger.d(TAG, "JPush用户注册成功");
        
   } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
       //Logger.d(TAG, "接受到推送下来的自定义消息");
       String title = bundle.getString(JPushInterface.EXTRA_TITLE);
       //Logger.d(TAG, " title : " + title);
       String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
       //Logger.d(TAG, "message : " + message);
       MyPreference.getInstance(context).setDataWithKey("message", message);
                
   } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
       //Logger.d(TAG, "接受到推送下来的通知");
       receivingNotification(context,bundle);

   } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
       //Logger.d(TAG, "用户点击打开了通知");
      openNotification(context,bundle);

   } else {
       //Logger.d(TAG, "Unhandled intent - " + intent.getAction());
   }
}

private void receivingNotification(Context context, Bundle bundle){
   String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
   //Logger.d(TAG, " title : " + title);
   String message = bundle.getString(JPushInterface.EXTRA_ALERT);
   //Logger.d(TAG, "message : " + message);
   String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
   //Logger.d(TAG, "extras : " + extras);
}

private void openNotification(Context context, Bundle bundle){
   String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
   String myValue = "";
   try {
       JSONObject extrasJson = new JSONObject(extras);
       myValue = extrasJson.optString("myKey");
   } catch (Exception e) {
//       Logger.w(TAG, "Unexpected: extras is not a valid json", e);
       return;
   }

}}
